package com.ray.util;

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.poi.util.IOUtils;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.ray.bean.MailConfigBean;

public class MailUtil {
	
	/**
	 * 
	 * @param toAddrs 邮件接收人的邮箱集合
	 * @param ccAddrs 邮箱接受抄送人邮箱集合
	 * @param subject 标题
	 * @param htmlText 具体内容
	 * @throws Exception
	 */
	public static void sendMailHtml(List<String> toAddrs,List<String> ccAddrs,String subject,String htmlText) throws Exception{
		String[] toAddrArry = new String[toAddrs.size()];
		String[] ccAddrArry=new String[ccAddrs.size()];
		toAddrs.toArray(toAddrArry);
		ccAddrs.toArray(ccAddrArry);
		MailUtil.sendMailHtml(toAddrArry, ccAddrArry, subject, htmlText);
	}
	
	public static void sendMailHtml(String[] toAddrs,String[] ccAddrs,String subject,String htmlText) throws Exception {
		JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
		// 设定 Mail Server
		senderImpl.setHost("smtp.qq.com");
		senderImpl.setPort(587);
		//SMTP验证时，需要用户名和密码
		senderImpl.setUsername("2417283690@qq.com");
		senderImpl.setPassword("zijdxnfowvfgecdg");
		// 建立邮件讯息
		MimeMessage msg = senderImpl.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(msg, true,"utf-8");
		// 设定收件人、寄件人、主题与内文
		helper.setTo(toAddrs);
		if(ccAddrs!=null&&ccAddrs.length>0)helper.setCc(ccAddrs);

		helper.setFrom("shinehao.mail@foxmail.com");
		helper.setSubject(subject);
		helper.setText(htmlText, true);
		// 传送邮件
		senderImpl.send(msg);
		System.out.println("邮件传送OK..");		
	}
	
	/**
	 * 公司发送邮箱类型
	 * @param toAddrs 邮件接收人的邮箱集合
	 * @param ccAddrs 邮箱接受抄送人邮箱集合
	 * @param subject 标题
	 * @param htmlText 具体内容
	 * @param mailConfigBean 公司配置
	 * @throws MessagingException 
	 */
	public static void sendCompanyMailHtml(String[] toAddrs,String[] ccAddrs,String subject,String htmlText,MailConfigBean mailConfigBean) throws MessagingException   {
		JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
		// 设定 Mail Server
		senderImpl.setHost(mailConfigBean.getHost());
		senderImpl.setPort(mailConfigBean.getPort());
		//SMTP验证时，需要用户名和密码
		senderImpl.setUsername(mailConfigBean.getEmail());
		senderImpl.setPassword(mailConfigBean.getPassword());
		// 建立邮件讯息
		MimeMessage msg = senderImpl.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(msg, true,"utf-8");
		// 设定收件人、寄件人、主题与内文
		helper.setTo(toAddrs);
		if(ccAddrs!=null&&ccAddrs.length>0)helper.setCc(ccAddrs);
		
		helper.setFrom(mailConfigBean.getEmail());
		helper.setSubject(subject);
		helper.setText(htmlText, true);
		// 传送邮件
		senderImpl.send(msg);
		System.out.println("邮件传送OK..");		
	}
	/**
	 * 公司发送邮箱类型
	 * @param toAddrs 邮件接收人的邮箱集合
	 * @param ccAddrs 邮箱接受抄送人邮箱集合
	 * @param subject 标题
	 * @param htmlText 具体内容
	 * @param mailConfigBean 公司配置
	 * @throws MessagingException 
	 */
	public static void sendCompanyMailHtml(String[] toAddrs,String[] ccAddrs,String subject,String htmlText,MailConfigBean mailConfigBean,String fileName,String remoteUrl) throws MessagingException   {
		JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
		// 设定 Mail Server
		senderImpl.setHost(mailConfigBean.getHost());
		senderImpl.setPort(mailConfigBean.getPort());
		//SMTP验证时，需要用户名和密码
		senderImpl.setUsername(mailConfigBean.getEmail());
		senderImpl.setPassword(mailConfigBean.getPassword());
		// 建立邮件讯息
		MimeMessage msg = senderImpl.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(msg, true,"utf-8");
		// 设定收件人、寄件人、主题与内文
		helper.setTo(toAddrs);
		if(ccAddrs!=null&&ccAddrs.length>0)helper.setCc(ccAddrs);
		
		helper.setFrom(mailConfigBean.getEmail());
		helper.setSubject(subject);
		helper.setText(htmlText, true);
		//helper.addAttachment("1.svg", dataSource);
		
		if(fileName!=null&&!"".equals(fileName)) {
			URL url=null;  
		    URLConnection urlconn=null;  
		    HttpURLConnection httpconn=null; 
		    try {
		    	url = new URL(remoteUrl);  
		    	urlconn = url.openConnection();  
		    	httpconn = (HttpURLConnection) urlconn;
		    	helper.addAttachment(fileName,new ByteArrayResource(IOUtils.toByteArray(  httpconn.getInputStream())));
		    }catch (Exception e) {
				// TODO: handle exception
		    	e.printStackTrace();
			}finally {
				
			}
		}
		// 传送邮件
		senderImpl.send(msg);
		System.out.println("邮件传送OK..");		
	}
	
}
